var base_url = "http://127.0.0.1:8080";

function log_out() {
    localStorage.removeItem("token");
    window.location.href = "../user/login.html";
}

function alert_to_user(content) {
    $.alert({
        title: '用户提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary'
            }
        }
    });
}

function confirm_to_user(content, func) {
    $.alert({
        title: '用户提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary',
                action: function () {
                    func();
                }
            },
            cancel: {
                text: '取消',
                action: function () {
                    $.alert('操作已取消!');
                }
            }
        }
    });
}

function alert_and_redirect(content, url, timeout) {
    $.alert({
        title: '用户提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary'
            }
        }
    });
    setTimeout(function () {
        window.location.href = url;
    }, timeout * 1000);
}

function pay_confirm(money, func) {
    $.alert({
        title: '用户提示',
        content: "请扫码支付：<b>" + money.toFixed(2) + "元</b><img src='/static/pay.png' alt=''>",
        buttons: {
            confirm: {
                text: '已支付',
                btnClass: 'btn-primary',
                action: function () {
                    func();
                }
            },
            cancel: {
                text: '取消',
                action: function () {
                    $.alert('操作已取消!');
                }
            }
        }
    });
}



function load_page_nav(page_sum, page) {
    if (page_sum <= 0 || page <= 0) {
        document.getElementById("page_nav-ul").innerHTML = "<li class=\"disabled\"><a href='#'><span><i class=\"mdi mdi-chevron-left\"></i></span></a></li><li class=\"disabled\"><a href='#'><span><i class=\"mdi mdi-chevron-right\"></i></span></a></li>";
        return;
    }
    var pageNavString = "";
    //在第一页
    if (page === 1) {
        pageNavString += "<li class=\"disabled\">" +
            "  <a href='#'>" +
            "     <span><i class=\"mdi mdi-chevron-left\"></i></span>" +
            "  </a>" +
            "</li>";
        pageNavString += "<li class=\"active\"><a href='#'>1</a></li>";
    } else {
        pageNavString += "<li class=\"disabled\">\n" +
            "  <a onclick=\"get_page(" + (page - 1) + ")\" style=\"cursor: pointer\" href='#'>\n" +
            "     <span><i class=\"mdi mdi-chevron-left\"></i></span>\n" +
            "  </a>\n" +
            "</li>";
        pageNavString += "<li><a onclick=\"get_page(" + (1) + ")\" style=\"cursor: pointer\" href='#'>1</a></li>";
    }
    for (var i = 2; i < page_sum; i++) {
        if ((page - 3) === i) {
            pageNavString += "<li class=\"disabled\"><a href='#'>...</a></li>";
        }
        if ((page - 3 < i) && (i < page + 3)) {
            if (page === i) {
                pageNavString += "<li class=\"active\"><a href='#' onclick=\"get_page(" + i + ")\" style=\"cursor: pointer\">" + i + "</a></li>";
            } else {
                pageNavString += "<li><a href='#' onclick=\"get_page(" + i + ")\" style=\"cursor: pointer\">" + i + "</a></li>";
            }
        }
        if ((page + 3) === i) {
            pageNavString += "<li class=\"disabled\"><a href='#'>...</a></li>";
        }
    }
    //在最后一页
    if (page_sum === page) {
        if (page_sum > 1) {
            pageNavString += "<li class=\"active\"><a href='#'>" + page_sum + "</a></li>";
        }
        pageNavString += "<li class=\"disabled\">\n" +
            "  <a href='#'>" +
            "     <span><i class=\"mdi mdi-chevron-right\"></i></span>\n" +
            "  </a>\n" +
            "</li>";
        pageNavString += "</div>";
    } else {  //不在最后一页
        if (page_sum > 1) {
            pageNavString += "<li><a href='#' onclick=\"get_page(" + page_sum + ")\" style=\"cursor: pointer\">" + page_sum + "</a></li>";
        }
        pageNavString += "<li>" +
            "  <a href='#' onclick=\'get_page(" + (page + 1) + ")\' style=\'cursor: pointer\'>\n" +
            "     <span><i class=\"mdi mdi-chevron-right\"></i></span>\n" +
            "  </a>" +
            "</li>";
    }
    document.getElementById("page_nav-ul").innerHTML = pageNavString;
}